package com.lmy.product.controller;

import com.lmy.common.utils.PageUtils;
import com.lmy.common.utils.R;
import com.lmy.product.entity.AttrEntity;
import com.lmy.product.entity.AttrGroupEntity;
import com.lmy.product.service.AttrAttrgroupRelationService;
import com.lmy.product.service.AttrGroupService;
import com.lmy.product.service.AttrService;
import com.lmy.product.service.CategoryService;
import com.lmy.vo.AttrGroupAttrsVo;
import com.lmy.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author lmy
 * @email lmy06091@163.com
 * @date 2024-01-13 15:44:49
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    ///product/attrgroup/{catelogId}/withattr
    @GetMapping("/{catelogId}/withattr")
    public  R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId){
        List<AttrGroupAttrsVo> vos = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data",vos);
    }


    /**
     * 新建关联
     * @param vos
     * @return
     */
    @PostMapping("/attr/relation")
    public R attrRelation(@RequestBody AttrGroupRelationVo[] vos){
        attrAttrgroupRelationService.attrRelation(vos);
        return R.ok();
    }

    @GetMapping("{attrgroupId}/noattr/relation")
    public R noAttrRelation (@PathVariable("attrgroupId") Long attrgroupId,
                             @RequestParam Map<String, Object> params){
       PageUtils page =  attrService.noAttrRelation(attrgroupId,params);
       return R.ok().put("page",page);
    }


    /**
     * 删除属性与分组的关联关系
     * @param vos
     * @return
     */
    @PostMapping("attr/relation/delete")
    public R attrRelationDelete(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return  R.ok();
    }

    /**
     * 根据分组id查找关联的所有基本属性
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> attrEntityList = attrService.attrRelation(attrgroupId);

        return R.ok().put("data", attrEntityList);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
        //  PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long catelogId = attrGroup.getCatelogId();
        Long[] path = categoryService.findCatelogPath(catelogId);
        attrGroup.setCatelogPath(path);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
